
#include <librally.h>

void* rp_rindexd_dispatch(void* pArg)
{
	Err err;
	char fname[] = "rp_rindexd_dispatch";
	char msg[] = "fatal error";
 
    /* Request socket. */    
	int dispatch_fd = socket(PF_INET, SOCK_STREAM, 0);    
	if (s < 0) 
	{        
		rp_log_rindexd(LOG_FATAL, fname, msg);        
	}    

	struct sockaddr_in dispatch_addr;   
	bzero(&dispatch_addr, sizeof(dispatch_addr));    
	dispatch_addr.sin_family = AF_INET;    
	dispatch_addr.sin_port = htons(g_port_rindexd_dispatch);    
	dispatch_addr.sin_addr.s_addr = INADDR_ANY;  		// TODO: Multiple network card support

	/* bind & listen */    
	if (bind(dispatch_fd, (struct sockaddr *) &dispatch_addr, sizeof(dispatch_addr)) < 0) 
	{        
		rp_log_rindexd(LOG_FATAL, fname, msg);
	}    
 
	if (listen(dispatch_fd, g_backlog_rindexd_dispatch) < 0) 
	{       
		rp_log_rindexd(LOG_FATAL, fname, msg);
	}    

	err = rp_rstatd_queue_init(g_queue_length_rindexd_dispatch);
	err = rp_threadpool_init(g_pool_size_rindexd_dispatch, rp_thread_t * pTid [ ], ThreadFunc pFunc [ ], void * pArg [ ]);
	
	for (;;)
	{
		/*	malloc fd and sockaddr for each new rpointd connection, dispatch thread must free them later	*/
		int* pClient_fd = (int*)malloc(sizeof(int));
		struct sockaddr_in* pClient_addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
		int len,
		rp_thread_t tid_dispatch;

		*pClient_fd = accept(dispatch_fd, pClient_addr, &len);

		// create a new task and put it in the queue
		Task* pTask = (Task*)malloc(sizeof(Task));
		
		err = rp_queue_put(pTask); 		// TODO: Wrong
	}

}

